package com.hp.attendance.service;

import com.hp.attendance.dao.MenuDao;
import com.hp.attendance.entity.Menu;
import com.hp.attendance.entity.PageInfo;
import com.hp.attendance.entity.dto.MenuDto;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuService {
    private MenuDao menuDao = new MenuDao();

    public List<Menu> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return menuDao.getList();
    }

    public List<MenuDto> getMenuByEmpId(int empId) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        List<MenuDto> list = new ArrayList<MenuDto>();
        //先查询出顶级菜单
        List<Menu> menus = menuDao.getMenuByEmpId(empId, 0);
        //设置顶级菜单的子菜单
        for (Menu menu : menus) {
            MenuDto menuDto = new MenuDto();
            int id = menu.getId();
            List<Menu> menusChlid = menuDao.getMenuByEmpId(empId, id);
            menuDto.setMenus(menu);
            menuDto.setChilds(menusChlid);
            list.add(menuDto);
        }
        return list;
    }

    /**
     * 分页查询菜单列表
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public PageInfo<MenuDto> getPage(int pageNo ,int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<MenuDto> page = new PageInfo<MenuDto>();
        //查询分页的数据                           2       10
        List<Menu> list = menuDao.getLimitList(pageNo, pageSize);
        List<MenuDto> listMenu=new ArrayList<MenuDto>();
        //遍历list
        for (Menu menu : list) {
            MenuDto menuDto = new MenuDto();
            int pid = menu.getPid();
            //pmenu是menu的父菜单对象
            Menu pmenu = menuDao.getMenu(pid);
            menuDto.setMenus(menu);
            menuDto.setPmenu(pmenu);
            listMenu.add(menuDto);
        }
        page.setRecords(listMenu);
        //查询总记录条数
        long total = menuDao.getTotal();
        page.setTotal(total);
        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

    public void addMenu(Menu menu) throws SQLException, IOException, ClassNotFoundException {
        menuDao.addMenu(menu);
    }

    public Menu getMenu(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return menuDao.getMenu(id);
    }

    public  void updateMenu(Menu menu) throws SQLException, IOException, ClassNotFoundException {
        menuDao.updateMenu(menu);
    }


    public void deleteMenu(int id) throws SQLException, IOException, ClassNotFoundException {
        menuDao.deleteMenu(id);
    }

    public List<Menu> getMenusByRoleId(int roleId) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return menuDao.getMenusByRoleId(roleId);
    }
}
